package handler.client;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import handler.dataPackage.DriverDeviceMessage;
import handler.dataPackage.PlatResponse;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

/**
 * @author dfj@99chuxing.com
 * @date 2017/12/13 0013 17:47
 */
public class ClientHandler extends ChannelInboundHandlerAdapter {

    /**
     * 在到服务器的连接已经建立之后将被调用
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("通道激活----，开始发送信息");
        //当被通知 Channel是活跃的时候，发送一条消息
//        DriverDeviceMessage driverDeviceMessage =new DriverDeviceMessage();
//        driverDeviceMessage.setData("这是第一条测试数据---通道激活---");
//        driverDeviceMessage.setProtoType("TCP");
//        driverDeviceMessage.setDeviceId("111111111111111111111111");
//        String driverMessageString = JSONObject.toJSONString(driverDeviceMessage);
        String driverMessageString = "{\"data\":{\"bookOrderId\":\"\",\"orderId\":\"\",\"locList\":[{\"time\":\"20180404154652\",\"direction\":263,\"lng\":113.698433,\"lat\":34.798457,\"speed\":0.0,\"locType\":3},{\"time\":\"20180404154654\",\"direction\":263,\"lng\":113.698433,\"lat\":34.798457,\"speed\":0.0,\"locType\":3},{\"time\":\"20180404154656\",\"direction\":263,\"lng\":113.698433,\"lat\":34.798457,\"speed\":0.0,\"locType\":3},{\"time\":\"20180404154658\",\"direction\":263,\"lng\":113.698433,\"lat\":34.798457,\"speed\":0.0,\"locType\":3},{\"time\":\"20180404154700\",\"direction\":263,\"lng\":113.698433,\"lat\":34.798457,\"speed\":0.0,\"locType\":3}],\"locType\":3,\"locCount\":5,\"status\":1},\"deviceId\":\"450221198709016011\",\"protoType\":\"0007\"}\n";
//        String driverMessageString = "hello world";
        ctx.channel().writeAndFlush(driverMessageString);
    }


    /**
     *  当从服务器接收到一条消息时被调用
     * @param channelHandlerContext
     * @param o
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
        PlatResponse driverDeviceMessage =(PlatResponse) o;
        System.out.println("打印服务端返回的数据 "+driverDeviceMessage.getData());
        Thread.sleep(2000);
//        DriverDeviceMessage driverDeviceMessage1 =new DriverDeviceMessage();
//        driverDeviceMessage.setData("这是第二条条测试数据---收到服务端消息-------");
//        // gps 上传标识
//        driverDeviceMessage.setProtoType("0007");
//        driverDeviceMessage.setDeviceId("22222222222222222222222222");
        String driverMessageString = "{\"data\":{\"bookOrderId\":\"\",\"orderId\":\"\",\"locList\":[{\"time\":\"20180404154652\",\"direction\":263,\"lng\":113.698433,\"lat\":34.798457,\"speed\":0.0,\"locType\":3},{\"time\":\"20180404154654\",\"direction\":263,\"lng\":113.698433,\"lat\":34.798457,\"speed\":0.0,\"locType\":3},{\"time\":\"20180404154656\",\"direction\":263,\"lng\":113.698433,\"lat\":34.798457,\"speed\":0.0,\"locType\":3},{\"time\":\"20180404154658\",\"direction\":263,\"lng\":113.698433,\"lat\":34.798457,\"speed\":0.0,\"locType\":3},{\"time\":\"20180404154700\",\"direction\":263,\"lng\":113.698433,\"lat\":34.798457,\"speed\":0.0,\"locType\":3}],\"locType\":3,\"locCount\":5,\"status\":1},\"deviceId\":\"450221198709016011\",\"protoType\":\"0007\"}\n";
        channelHandlerContext.writeAndFlush(driverMessageString);
    }

    /**
     *在处理过程中引发异常时被调用
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
